<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of controller
 *
 * @author phamhphuc
 */

class referer_action_controller extends system_controller {
    public function indexAction()
    {                                                     
        if(!isset($_REQUEST['isliked']) && ($_SERVER['REMOTE_ADDR'] != "127.0.0.1")){
            if(!$this->config['facebookstatus']){
                $this->_frontredirect("requirelike");                
            }                        
                        
            if($this->config['facebookstatus'] && ($this->config['facebookstatus']['page']['liked'] != 1)) {
                $this->session->is_required_liked = time();
                $this->_frontredirect("requirelike");
            } else {                
                if($this->session->is_required_liked) {
                    $this->db->insert("#__statistic_like", array(
                        'client_id' => $this->config['user_id'],
                        'date_time' => time(),
                        'ip' => $_SERVER['REMOTE_ADDR']
                    ));
                    $this->session->is_required_liked = null;
                }
            }
        }
                
        // log number of app views
        $this->db->insert("#__statistic_appview", array(
            'client_id' => $this->config['user_id'],
            'date_time' => time(),
            'ip' => $_SERVER['REMOTE_ADDR']
        ));        

        // check if system is turn off by user
        if(($this->settings['turn_front_end_on_off'] == 0) && ($_GET['action'] != "systenoff")){
            $this->_frontredirect("systenoff");            
        }
    }
    
    
    public function questionAction()
    {
        if(count($_POST) > 0){
            $data = $_POST['data'];
            $data['update_time'] = time();
            $data['ip'] = $_SERVER['REMOTE_ADDR'];
            $data['user_id'] = $this->config['user_id'];           
            $this->db->insert("#__answer", $data);            
            echo 1;die;            
        }
        
        $num = $_GET['day'];
        $sql = "select * from #__question_day where day_num = {$num} and user_id = {$this->config['user_id']}";
        $question = $this->db->getRow($sql);
        $this->view->question = $question;
        $this->_setNoLayout();
        
        // log number of question views
        $this->db->insert("#__statistic_dooropen", array(
            'client_id' => $this->config['user_id'],
            'date_time' => time(),
            'door_num' => $num,
            'ip' => $_SERVER['REMOTE_ADDR']
        ));
    }
    
    public function submitdataAction()
    {
        $data = $this->_filterTextArr($_POST['data']);        
        
        $this->db->insert("#__answer", array(
            'name' => @$data['name'],
            'email' => @$data['email'],
            'data' => serialize($data),
            'update_time' => time(),
            'ip' => $_SERVER['REMOTE_ADDR'],
            'user_id' => $this->config['user_id']
        ));
        // update statistic
        $this->db->insert("#__statistic_contributions", array(
                        'client_id' => $this->config['user_id'],
                        'date_time' => time(),
                        'ip' => $_SERVER['REMOTE_ADDR']
                    ));
        die("done");
    }        
    
    public function notsetupAction()
    {
        die("The app is not set up correctly, please contact app provider to fix this issue");
    }

    public function youtubeAction()
    {
        
    }
    
    public function konkurranseAction()
    {
        
    }
    
    public function skjemaAction(){
        
    }

    public function getstatsAction()
    {
        $token = $_GET['token'];
        if($token == "asdf876asdfhihausyfd") {
            $result = array(
                array(
                    'key_title' => 'Contribution number',
                    'key_value' => $this->db->getOne("select count(*) as total from #__answer")
                ),
                array(
                    'key_title' => 'App views',
                    'key_value' => $this->db->getOne("select count(*) as total from #__statistic_appview")
                ),                
                array(
                    'key_title' => 'Total users',
                    'key_value' => $this->db->getOne("select count(*) as total from #__admin_user")
                ),
                array(
                    'key_title' => 'New likes',
                    'key_value' => $this->db->getOne("select count(*) as total from #__statistic_like")
                )
            );
            $data = array(
                'result' => $result
            );
            header ('Access-Control-Allow-Origin: *');
            echo 'slopetracker(' . json_encode($data) . ')';die;
        } else {
            die("access deny");
        }

    }

    public function requirelikeAction()
    {
        $this->_setNoLayout();
    }
    
    public function systenoffAction()
    {
        $this->_setNoLayout();
    }

    public function tosAction(){
        // log number of TOS views
        $this->db->insert("#__statistic_tosclick", array(
            'client_id' => $this->config['user_id'],
            'date_time' => time(),
            'ip' => $_SERVER['REMOTE_ADDR']
        ));
        $this->_setNoLayout();
    }

    public function addtabAction()
    {

    }

    public function fblikeviewAction()
    {
        $this->_setNoLayout();          
//        echo $this->config['facebooktab_share_url'];die;
    }

    public function getTemplateDir(){

        if($this->config['template_name']) {           
            return "../../templates/{$this->config['template_name']}/";
        }
        return "";
    }

    public function getTemplateURL(){
        if($this->config['template_name']) {
            if($this->config['use_aws_template_storage']){                
                return "{$this->config['aws_template_url']}{$this->config['template_name']}/";
            }
            return "../../templates/{$this->config['template_name']}/";
        }
        return "";
    }
}

?>
